<?php
// +----------------------------------------------------------------------
// | webhooks
// +----------------------------------------------------------------------
// | Author: King east <1207877378@qq.com>
// +----------------------------------------------------------------------


namespace ke;


class Ke
{
    static $HOST = [
        'http://localhost:8080',
        'http://webhooks.cysmsc.cn',
    ];

    public static function bootstrap()
    {
        $origin = $_SERVER['HTTP_ORIGIN'] ?? '';
        if (in_array($origin, static::$HOST)) {
            header('Access-Control-Allow-Origin: ' . $origin);
        }

        header('Access-Control-Allow-Methods:GET,POST,PATCH,PUT,DELETE');
        header('Access-Control-Allow-Headers:Authorization, Content-Type, X-Requested-With, X-Driver');
        header('Access-Control-Allow-Credentials: true');
        if (is_options()) {
            exit;
        }

        Config::init();

        // 异常托管
        $whoops = new \Whoops\Run();
        $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler);
        $whoops->register();

        // 路由初始化
        Route::init();
        $files = glob(ROOT_PATH . 'route/*.php');
        foreach ($files as $file) require($file);

        Route::displatch();

        // ORM 初始化
        \think\Db::setConfig(Config::get('db'));

    }
}